﻿using System;
using Tfs.OnTheRoad.ViewModel;
namespace Tfs.OnTheRoad.Commands
{
    /// <summary>
    /// Command that performs the LoadMore action on Listboxes
    /// </summary>
    public class LoadMoreItemsCommand : BaseCommand<BaseViewModel>
    {
        public LoadMoreItemsCommand(BaseViewModel owner)
            : base(owner)
        {
            Owner.PropertyChanged += (s, e) =>
            {
                if (CanExecuteChangedSetted && string.Compare(e.PropertyName, "LoadMoreResultsVisible") == 0)
                {
                    raiseCanExecuteChanged(this, EventArgs.Empty);
                }
            };
        }
        
        public override bool CanExecute(object parameter)
        {
               if (Owner is IBaseListViewModel)
            {
                return ((IBaseListViewModel)Owner).LoadMoreResultsVisible;
            }
               else
                   return false;
        }

        public override void Execute(object parameter)
        {

            if (Owner is IBaseListViewModel)
            {
                ((IBaseListViewModel)Owner).LoadNextPage();
            }                 
        }
    }
}